<?php
/****************************************************
Wer Wird Millionär
TIT10NS Webengeneering Projekt

changecategory - Seite um Kategorie zu ändern

Aufgeteilt in zwei Formulare:
-Das Erste ist zuständig für die Auswahl der Kategorie
-Das Zweite nutzt das selbe Formular wie 
 newcategory.php um die Frage zu bearbeiten

Autoren: Markus Schwarz
Copyright: 2012/2013 DHBW Mannheim
****************************************************/
include ("inc/startup.php");
include ("inc/header.php");
require ("inc/TAdminCategory.php");
require ("inc/TAdminView.php");
		
if (isset($_POST['submitChoice'])) {
	if (isset($_POST['category'])) {
		$category = new TAdminCategory($_POST['category'], TModel::getCategoryByCategoryID($_POST['category']));
		$notification = new TAdminNotification("", "");

		TAdminView::getCategoryForm($category, $notification);
	}
	else {
		$notification = new TAdminNotification("error","changeCategoryFormNoSelection");
		TAdminView::getChangeCategoryForm($notification);
	}
}
else {
	if (isset($_POST['catButton'])) {
		if ($_POST['catName'] != "") {
			if (TModel::getCategoryByCategoryID($_POST['catID']) != $_POST['catName']) {
				$category = new TAdminCategory($_POST['catID'], $_POST['catName']);
				if (TModel::checkCategoryName($category)) {
					$category = new TAdminCategory($_POST['catID'], "");
					$notification = new TAdminNotification("error", "changeCategoryFormNameGiven");

					TAdminView::getCategoryForm($category, $notification);
				}
				else {
					$category = new TAdminCategory($_POST['catID'], $_POST['catName']);
					$questions = $_POST['questions'];
					
					TModel::updateCategory($category, $questions);
					
					$notification = new TAdminNotification("success", "changeCategoryFormUpdateSuccess");
					TAdminView::getCategoryForm($category, $notification);
				}
			}
			else {
				// Keine Änderung am Namen
				$category = new TAdminCategory($_POST['catID'], $_POST['catName']);
				$questions = $_POST['questions'];

				TModel::updateCategory($category, $questions);
					
				$notification = new TAdminNotification("success", "changeCategoryFormUpdateSuccess");
				TAdminView::getCategoryForm($category, $notification);
			}
		}
		else {
			$category = new TAdminCategory($_POST['catID'], "");
			$notification = new TAdminNotification("error", "changeCategoryFormTextEmpty");

			TAdminView::getCategoryForm($category, $notification);
		}
	}
	else {
		$notification = new TAdminNotification("","");
		TAdminView::getChangeCategoryForm($notification);
	}
}

include ("inc/footer.php");
?>
